package com.im.interceptor;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;

@Configuration
@ComponentScan(basePackages = "com.im.service.*")
public class TransactionConfigInterceptor {

	private static final Logger logger = LogManager.getLogger(TransactionConfigInterceptor.class);

	@Autowired
	private DataSourceTransactionManager transactionManager;

	// 创建事务通知
	@Bean(name = "txAdvice")
	public TransactionInterceptor getAdvisor() throws Exception {
		Properties properties = new Properties();
		properties.setProperty("get*", "PROPAGATION_REQUIRED,-Exception,readOnly");
		properties.setProperty("query*", "PROPAGATION_REQUIRED");
		properties.setProperty("add*", "PROPAGATION_REQUIRED");
		properties.setProperty("insert*", "PROPAGATION_REQUIRED");
		properties.setProperty("save*", "PROPAGATION_REQUIRED");
		properties.setProperty("update*", "PROPAGATION_REQUIRED");
		properties.setProperty("delete*", "PROPAGATION_REQUIRED");
		TransactionInterceptor tsi = new TransactionInterceptor(transactionManager, properties);
		return tsi;
	}

	@Bean
	public BeanNameAutoProxyCreator txProxy() {
		BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
		creator.setInterceptorNames("txAdvice");
		creator.setBeanNames("*SMO", "*SMOImpl");
		creator.setProxyTargetClass(true);
		return creator;
	}

	@Bean
	public DataSourceTransactionManager transactionManager(DataSource dataSource) {
		logger.info(" ============dataSource");

		return new DataSourceTransactionManager(dataSource);
	}
//
//	@Bean
//	public TransactionInterceptor transactionInterceptor(DataSourceTransactionManager transactionManager) {
//		logger.info("===================transactionManager");
//		TransactionInterceptor ti = new TransactionInterceptor();
//		ti.setTransactionManager(transactionManager);
//		Properties properties = new Properties();
//		properties.setProperty("get*", "PROPAGATION_REQUIRED,readOnly");
//		properties.setProperty("add*", "PROPAGATION_REQUIRED");
//		properties.setProperty("del*", "PROPAGATION_REQUIRED");
//		properties.setProperty("save*", "PROPAGATION_REQUIRED");
//		properties.setProperty("remove*", "PROPAGATION_REQUIRED");
//		properties.setProperty("update*", "PROPAGATION_REQUIRED");
//		// properties.setProperty("*", "PROPAGATION_REQUIRED,readOnly");
//		ti.setTransactionAttributes(properties);
//		return ti;
//	}
//
//	// 代理到ServiceImpl的Bean
//	@Bean
//	public BeanNameAutoProxyCreator transactionAutoProxy() {
//		logger.info("===========BeanNameAutoProxyCreator");
//		BeanNameAutoProxyCreator transactionAutoProxy = new BeanNameAutoProxyCreator();
//		transactionAutoProxy.setProxyTargetClass(true);
//		transactionAutoProxy.setBeanNames(new String[] { "*SMOImpl", "*SMO" });
//		transactionAutoProxy.setInterceptorNames("transactionInterceptor");
//		return transactionAutoProxy;
//	}
}
